package model

import (
	"common/util"
	"errors"
	"fmt"
	g "github.com/doug-martin/goqu/v9"
)

type ConfigProxyInviteBonusParams struct {
	ValidCounts uint32  `json:"validCounts" cbor:"validCounts" db:"valid_Counts" goqu:"validCounts"`
	Bonus       float64 `json:"bonus" cbor:"bonus" db:"bonus" goqu:"bonus"`
}
type ConfigProxyInviteBonus struct {
	Id uint32 `json:"id" cbor:"id" db:"id" goqu:"id"`
	ConfigProxyInviteBonusParams
}
type ConfigProxyInviteBonusPageParams struct {
	ConfigProxyInviteBonus
	util.PageParams
	Data []ConfigProxyInviteBonus `json:"data"`
}
type ConfigProxyInviteBonusModel struct {
	TableName string
}

const configProxyInviteBonusPrimaryKey = "id"

func (m ConfigProxyInviteBonusModel) GetEntityToEx(params ConfigProxyInviteBonus) g.Expression {
	ex := g.Ex{}
	if params.Bonus != 0 {
		ex["bonus"] = params.Bonus
	}
	if params.ValidCounts != 0 {
		ex["valid_Counts"] = params.ValidCounts
	}
	if params.Id != 0 {
		ex["id"] = params.Id
	}
	return ex
}
func (m ConfigProxyInviteBonusModel) Get(id uint32, ent interface{}) error {
	if id == 0 {
		return errors.New("参数错误")
	}
	ex := g.Ex{configProxyInviteBonusPrimaryKey: id}
	return m.GetByEx(ex, ent)
}
func (m ConfigProxyInviteBonusModel) GetByEx(ex g.Expression, ent interface{}) error {
	query, _, _ := dialect.From(m.TableName).Where(ex).Limit(1).ToSQL()
	fmt.Printf("query sql :{%s}\n", query)
	return meta.MerchantDB.Get(ent, query)
}
func (m ConfigProxyInviteBonusModel) CountByEx(ex g.Expression) (uint32, error) {
	from := dialect.From(m.TableName).Select(g.COUNT(configProxyInviteBonusPrimaryKey))
	if ex != nil {
		from = from.Where(ex)
	}
	query, _, err := from.ToSQL()
	var total int
	if err = meta.MerchantDB.Get(&total, query); err != nil {
		return 0, err
	}
	return uint32(total), nil
}
func (m ConfigProxyInviteBonusModel) Count() (uint32, error) {
	return m.CountByEx(nil)
}
func (m ConfigProxyInviteBonusModel) Page(page *ConfigProxyInviteBonusPageParams) error {
	return m.PageByEx(page, m.GetEntityToEx(page.ConfigProxyInviteBonus))
}
func (m ConfigProxyInviteBonusModel) PageByEx(page *ConfigProxyInviteBonusPageParams, ex g.Expression) error {
	total, err := m.CountByEx(ex)
	if err != nil {
		return err
	}
	page.Total = total
	currentPage := page.CurrentPage
	if currentPage < 1 {
		currentPage = 1
	}
	offset := (currentPage - 1) * page.PageSize
	if offset < 0 {
		offset = 0
	}
	pageSize := page.PageSize
	if pageSize <= 0 {
		pageSize = 10
	}
	form := dialect.From(m.TableName)
	if ex != nil {
		form = form.Where(ex)
	}
	query, _, _ := form.Offset(uint(offset)).Limit(uint(pageSize)).Order(g.C("valid_Counts").Asc()).ToSQL()
	fmt.Printf(" sql :{%s}\n", query)
	ent := make([]ConfigProxyInviteBonus, 0)
	err = meta.MerchantDB.Select(&ent, query)
	if err != nil {
		return err
	}
	page.CurrentPage = currentPage
	page.PageSize = pageSize
	page.Data = ent
	return nil
}
func (m ConfigProxyInviteBonusModel) DeleteById(id uint32) error {
	return m.DeleteByEx(g.Ex{configProxyInviteBonusPrimaryKey: id})
}
func (m ConfigProxyInviteBonusModel) DeleteByEx(ex g.Expression) error {
	if ex == nil {
		return errors.New("参数错误")
	}
	query, _, err := dialect.Delete(m.TableName).Where(ex).ToSQL()
	fmt.Printf("delete sql :{%s}\n", query)
	_, err = meta.MerchantDB.Exec(query)
	if err != nil {
		return err
	}
	return nil
}
func (m ConfigProxyInviteBonusModel) Update(entity ConfigProxyInviteBonus) error {
	query, _, err := dialect.Update(m.TableName).Set(&entity.ConfigProxyInviteBonusParams).Where(g.Ex{configProxyInviteBonusPrimaryKey: entity.Id}).ToSQL()
	if err != nil {
		return err
	}
	_, err = meta.MerchantDB.Exec(query)
	fmt.Printf("update sql :{%s}\n", query)
	if err != nil {
		return err
	}
	return nil
}
func (m ConfigProxyInviteBonusModel) Insert(entity ConfigProxyInviteBonusParams) error {
	query, _, err := dialect.Insert(m.TableName).Rows(&entity).ToSQL()
	if err != nil {
		return err
	}
	res, err := meta.MerchantDB.Exec(query)
	fmt.Printf("insert sql :{%s}\n", query)
	id, err := res.LastInsertId()
	if err != nil {
		return err
	}
	fmt.Printf("insert sql :{%d}\n", id)
	return nil
}
